var app = getApp()
import * as appConfig from "../../../app-config.js";

Page({

  data: {
    name:"",
    phone:"",
    password:"",
    sendVer:'发送验证码',
    verification:'',
    num:'',
    isAdd:false
  },  
  onLoad: function (options) {

  },
  bindSave(){
    this.saveAccount()
    
  },
  getVer(e){
    this.setData({
      verification:e.detail.value
    })
  },
  //保存账号
  saveAccount:function(){
    this.setData({
      isAdd:true
    })
    var that = this;
    var password = this.data.password;
    if (password=='') {
      app.errorMsg('请输入手机号')
      this.setData({
        isAdd:false
      })
    }else if (this.data.verification=='') {
      app.errorMsg('请输入验证码')
      this.setData({
        isAdd:false
      })
    }else if (password.length < 6) {
      wx.showToast({
        title: '密码至少6位',
        icon: "none"
      })
      this.setData({
        isAdd:false
      })
      return
    }else{
      app.getMemInfo().then(res => {
        var agentId = res.data.agentId;
        var accountId = res.data.id
        var url = 'wx_app/agent/account/add';
        var data = {
          agentId: agentId,
          name: this.data.name,
          phone: this.data.phone,
          password: password,
          accountId,
          code:this.data.verification
        }
        console.log(data)
        app.request(url, data, 'GET').then(res => {
          console.log(res)
          if (res.code==1) {
            this.saveSuccess(res);
          }else{
            app.errorMsg(res.message)
            this.setData({
              isAdd:false
            })
          }

        })
      })
    }
    
  },
  //发送验证码
  sendVer(){
    if (this.data.sendVer==='发送验证码') {
      if (this.data.phone=='') {
        app.errorMsg('请输入手机号')
      }else{
        let data = {
          phoneNum:this.data.phone
        }
        console.log(data)
        app.request('wx_app/sendSmsCode',data,'GET').then(res=>{
          console.log(res)
          if (res.code==0) {
            app.errorMsg(res.message)
          }
        })
        this.setData({
          num:120,
          sendVer:'120s后重新发送'
        })
        let time = setInterval(()=>{
          let num = this.data.num
          num --
          this.setData({
            num,
            sendVer:num+'s后重新发送'
          })

          if(num == 0){
           clearInterval(time)
           this.setData({
            sendVer:'发送验证码'
          })
         }
       },1000)
      }
    }else{

    }
  },
  saveSuccess:function(){
    wx.navigateBack({
      url: 'shopAccount'
    })
  },

//获取账号姓名
getNameValue:function(e){
  this.setData({
    name: e.detail.value
  })
},

  //获取
  getPhoneValue: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  
  getPswValue: function (e) {
    this.setData({
      password: e.detail.value
    })
  },


})